import 'base_model.dart';

/// 陶瓷作品模型
class Ceramic implements BaseModel {
  final int? id;
  final int userId; // 创建者ID
  final String? title; // 标题
  final String? description; // 描述
  final String? imageUrl; // 图片URL
  final bool isSkipped; // 是否跳过了烧制过程
  final bool isCompleted; // 是否已完成上传（用于标识不再显示在烧制页面）
  final int likeCount; // 点赞数
  final int favoriteCount; // 收藏数
  final DateTime createdAt; // 创建时间
  final DateTime updatedAt; // 更新时间

  Ceramic({
    this.id,
    required this.userId,
    this.title,
    this.description,
    this.imageUrl,
    this.isSkipped = false,
    this.isCompleted = false,
    this.likeCount = 0,
    this.favoriteCount = 0,
    required this.createdAt,
    required this.updatedAt,
  });

  @override
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'user_id': userId,
      'title': title,
      'description': description,
      'image_url': imageUrl,
      'is_skipped': isSkipped ? 1 : 0,
      'is_completed': isCompleted ? 1 : 0,
      'like_count': likeCount,
      'favorite_count': favoriteCount,
      'created_at': createdAt.millisecondsSinceEpoch,
      'updated_at': updatedAt.millisecondsSinceEpoch,
    };
  }

  factory Ceramic.fromJson(Map<String, dynamic> json) {
    return Ceramic(
      id: json['id'],
      userId: json['user_id'],
      title: json['title'],
      description: json['description'],
      imageUrl: json['image_url'],
      isSkipped: json['is_skipped'] == 1,
      isCompleted: json['is_completed'] == 1,
      likeCount: json['like_count'] ?? 0,
      favoriteCount: json['favorite_count'] ?? 0,
      createdAt: DateTime.fromMillisecondsSinceEpoch(json['created_at']),
      updatedAt: DateTime.fromMillisecondsSinceEpoch(json['updated_at']),
    );
  }

  // 复制对象并修改指定字段
  Ceramic copyWith({
    int? id,
    int? userId,
    String? title,
    String? description,
    String? imageUrl,
    bool? isSkipped,
    bool? isCompleted,
    int? likeCount,
    int? favoriteCount,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) {
    return Ceramic(
      id: id ?? this.id,
      userId: userId ?? this.userId,
      title: title ?? this.title,
      description: description ?? this.description,
      imageUrl: imageUrl ?? this.imageUrl,
      isSkipped: isSkipped ?? this.isSkipped,
      isCompleted: isCompleted ?? this.isCompleted,
      likeCount: likeCount ?? this.likeCount,
      favoriteCount: favoriteCount ?? this.favoriteCount,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }
}
